class Solution {
    int[] dx = {0, 0, 1, -1};
    int[] dy = {1, -1, 0, 0};
    int ret = 0,m,n;
    public int maxAreaOfIsland(int[][] grid) {
        m=grid.length;
        n=grid[0].length;
        boolean[][] vis=new boolean[m][n];
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(grid[i][j]==1){

                    ret=Math.max(ret,bfs(grid,vis,i,j));
                }
            }
        }
        return ret;


    }

    public int bfs(int[][] grid,boolean[][] vis,int i,int j){
        Queue<int[]> q=new LinkedList<>();
        vis[i][j]=true;
        int count=1;
        q.add(new int[]{i,j});
        while(!q.isEmpty()){
            int[] arr=q.poll();
            int x1=arr[0];
            int y1=arr[1];
            for(int k=0;k<4;k++){
                int x=x1+dx[k];
                int y=y1+dy[k];
                if(x>=0&&x<m&&y>=0&&y<n&&grid[x][y]==1&&!vis[x][y]){
                    count++;
                    vis[x][y]=true;
                    q.add(new int[]{x,y});
                }

            }
        }
        return count;

    }
}